package com.ftg.learn.backlearn.array;

import java.sql.SQLOutput;
import java.util.Arrays;

/**
 * 学习二维数组
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/3/7 下午4:34
 */
public class LearnArray02 {

    //    一维  一个维度 （一个轴上面连续）
//      二维 二个维度 (二个轴)
    public static void main(String[] args) {
//        int 2 组合
        int[] as = new int[2];
//        int 6 个元素
//        结绳计数法 10  二
        int[][] as1 = new int[2][3];
        //赋值
        as1[0][0] = 1;
        as1[0][1] = 1;
        as1[0][2] = 1;

        as1[1][0] = 1;
        as1[1][1] = 1;
        as1[1][2] = 1;

        int[][] as2 = new int[2][];
        //赋值
        as2[0] = new int[2];
        as2[0][0] = 34;
        as2[0][1] = 67;

        System.out.println(Arrays.toString(as2));
        System.out.println(Arrays.toString(as2[0]));
        System.out.println(Arrays.toString(as2[1]));
        int[][] as3 = new int[][]{{123, 456}, {678, 9, 10}};
        as3[1][0] = 45;

        int[][] arrs = new int[][]{{1, 2, 3, 4, 5}, {4, 5, 6, 7, 8, 9}};
        //1 arrs两个元素的差值 左联 | 右联
        System.out.println(Arrays.toString(leftJoin(arrs, JoinType.left)));
        System.out.println(Arrays.toString(leftJoin(arrs, JoinType.right)));

        int[][] arrs1 = {{1, 2, 3}, {2, 3, 4}, {4, 5, 2}, {3, 4, 5}};
        System.out.println(Arrays.toString(leftJoin1(arrs1, JoinType.left)));


        System.out.println(Arrays.toString(fullJoin(arrs1)));

        //2 内联

        //3 全联

    }

    /**
     * 左差集
     *
     * @param arrs
     * @return
     */
    public static int[] leftJoin(int[][] arrs) {
        assert arrs != null;
        int[] reint = new int[arrs[0].length];
        int index = 0;
        for (int i = 0; i < arrs[0].length; i++) {
            boolean ist = false;
            for (int j = 0; j < arrs[1].length; j++) {
                if (arrs[0][i] == arrs[1][j]) {
                    ist = true;
                    break;
                }
            }
            if (ist == false) {
//                左差集
                reint[index++] = arrs[0][i];
            }
        }
        return Arrays.copyOf(reint, index);
    }

    public static int[] leftJoin(int[][] arrs, JoinType join) {
        assert arrs != null;

        int[] first = null;
        int[] second = null;

        if (join == JoinType.left) {
            first = arrs[0];
            second = arrs[1];
        } else if (join == JoinType.right) {
            first = arrs[1];
            second = arrs[0];
        }

        int[] reint = new int[first.length];


        int index = 0;
        for (int i = 0; i < first.length; i++) {
            boolean ist = false;
            for (int j = 0; j < second.length; j++) {
                if (first[i] == second[j]) {
                    ist = true;
                    break;
                }
            }
            if (ist == false) {
                reint[index++] = first[i];
            }
        }
        return Arrays.copyOf(reint, index);
    }

    public static int[] leftJoin1(int[][] arrs, JoinType join) {
        assert arrs != null;

        int primaryNum = join == JoinType.left ? 0 : arrs.length - 1;
        int[] primary = arrs[primaryNum];
        int[] reint = new int[primary.length];

        int index = 0;
        for (int i = 0; i < primary.length; i++) {
            boolean ist = false;
            for (int j = 0; j < arrs.length; j++) {
                if (j == primaryNum) {
                    continue;
                }
                for (int x = 0; x < arrs[j].length; x++) {
                    if (primary[i] == arrs[j][x]) {
                        ist = true;
                        break;
                    }
                }
            }
            if (ist == false) {
                reint[index++] = primary[i];
            }
        }
        return Arrays.copyOf(reint, index);
    }

    public static String[] fullJoin(int[][] arrs) {
        StringBuilder stringBuilder = new StringBuilder("");
        for (int i = 0; i < arrs.length; i++) {
            for (int i1 = 0; i1 < arrs[i].length; i1++) {
                if (stringBuilder.indexOf(arrs[i][i1] + "") < 0) {
                    stringBuilder.append(arrs[i][i1]).append((i == arrs.length - 1 && i1 == arrs[i].length - 1) ? "" : ",");
                }
            }
        }
        System.out.println(stringBuilder.toString());
        String[] nums = stringBuilder.toString().split(",");
        return nums;
    }
}
